#!/usr/bin/perl
#
# Change password for a counter administrator
#
use Counter::CGI;
use CGI::Carp;
use Apache::Htpasswd;

$q = new Counter::CGI;

$admin=$q->param("adminaccount");
if (!$admin) { error("Admin account name not given"); }

$newpass = $q->param("newpass1");
if (!$newpass) { error("New password not given"); }

if ($q->param("newpass2") ne $newpass) {
    error("Password mismatch");
}
if (!$ENV{REMOTE_USER}) {
    error("Not logged in");
}

# warn "Changing password for $admin to $newpass\n"; 
#$pwf = new Apache::Htpasswd("$ENV{COUNTER_ROOT}/access/users");
$pwf = new Apache::Htpasswd("/home/counter/access/users");
$pwf->htpasswd($admin, $newpass, 1)
    || error("Change password failed. Admin account does not exist?");


print $q->redirect("/administration/");

sub error {

    my $msg = shift;

    print <<EoF;
Content-type: text/html; charset="utf-8"

<html><head><title>Password error</title>
</head><body>
<h1>Error in password change</h1>
The error was $msg
</body>
</html>
EoF
    exit(0);

}

    
